package cn.swust.designpatternlearningplatform.config;





import cn.swust.designpatternlearningplatform.Interceptor.AuthenticationInterceptor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
    @Resource
    AuthenticationInterceptor authenticationInterceptor;

    /**
     * 图片保存路径，自动从yml文件中获取数据
     *   示例： D:/images/
     */
    @Value("${upload_path}")
    private String filePath;
    /**
     * @description: 拦截器拦截设置
     * @author: Tang
     * @date: 2022/10/21 23:03
     * @param: [registry]
     * @return: void
     **/
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(authenticationInterceptor)
                .addPathPatterns("/**")
                .excludePathPatterns("/public/**","/images/**");
        WebMvcConfigurer.super.addInterceptors(registry);
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        /**
         * 配置资源映射
         * 意思是：如果访问的资源路径是以“/images/”开头的，
         * 就给我映射到本机的“D:/images/”这个文件夹内，去找你要的资源
         * 注意：D:/images/ 后面的 “/”一定要带上
         */
        registry.addResourceHandler("/images/**")
                .addResourceLocations("file:"+filePath);
    }

}
